<?php
/*
 * @Author: 乔
 * @Date: 2021-11-04 15:41:38
 * @LastEditTime: 2021-11-08 15:02:11
 * @LastEditors: 乔
 * @FilePath: \tp30\app\admin\controller\Message.php
 */

namespace app\admin\controller;

use mailer\tp6\Mailer;
use think\facade\Request;

class Message
{
    public function sendEmail()
    {
        $mailer = Mailer::instance();
        $mailer->from('963606242@qq.com', '官方指定邮箱')
            ->to('1922435457@qq.com')
            ->subject('纯文本测试')
            ->text('欢迎您使用think_mail 暗号')
            ->send();
    }
    public static function resPwdEmail($path, $code)
    {
        $mailer = Mailer::instance();
        $res = $mailer->from('963606242@qq.com', 'TP30官方指定邮箱')
            ->to($path)
            ->subject('恢复密码邮件')
            ->text('欢迎您访问TP30 您的暗号是' . $code)
            ->send();
        return $res;
    }
    /**
     * excel文件导出
     */
    public static function export($data, $title)
    {
        // $data = [
        //     ['title1' => '111', 'title2' => '222'],
        //     ['title1' => '111', 'title2' => '222'],
        //     ['title1' => '111', 'title2' => '222']
        // ];
        // $title = ['第一行标题', '第二行标题'];

        // Create new Spreadsheet object
        $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        // 方法一，使用 setCellValueByColumnAndRow
        //表头
        //设置单元格内容
        foreach ($title as $key => $value) {
            // 单元格内容写入
            $sheet->setCellValueByColumnAndRow($key + 1, 1, $value);
        }
        $row = 2; // 从第二行开始
        foreach ($data as $item) {
            $column = 1;
            foreach ($item as $value) {
                // 单元格内容写入
                $sheet->setCellValueByColumnAndRow($column, $row, $value);
                $column++;
            }
            $row++;
        }

        // 方法二，使用 setCellValue
        //表头
        //设置单元格内容
        // $titCol = 'A';
        // foreach ($title as $key => $value) {
        //     // 单元格内容写入
        //     $sheet->setCellValue($titCol . '1', $value);
        //     $titCol++;
        // }
        // $row = 2; // 从第二行开始
        // foreach ($data as $item) {
        //     $dataCol = 'A';
        //     foreach ($item as $value) {
        //         // 单元格内容写入
        //         $sheet->setCellValue($dataCol . $row, $value);
        //         $dataCol++;
        //     }
        //     $row++;
        // }

        // Redirect output to a client’s web browser (Xlsx)
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="01simple.xlsx"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');

        // If you're serving to IE over SSL, then the following may be needed
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
        header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header('Pragma: public'); // HTTP/1.0
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        exit;
    }
    /**
     * 读取excel文件内容
     */
    public function read()
    {
        $inputFileName = Request::file('logo');
        $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);
        // 方法二
        $sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
        // return $sheetData;
        $key_name = ['name', 'pwd', 'author', ''];
        $date = [];
        foreach ($sheetData as $v) {
            $date[] = array_combine($key_name, $v);
        }
        array_splice($date, 0, 1);
        foreach ($date as $key => $value) {
            $value = array_filter($value);
            $admin = \app\admin\model\Admin::create($value);
        }
    }
}
